﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace DALL.Progect
{
    /// <summary>
    /// 数据访问类:T_PUB_BRANCHFILE
    /// </summary>
    public partial class T_PUB_BRANCHFILE
    {
        public T_PUB_BRANCHFILE()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Progect.T_PUB_BRANCHFILE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_BRANCHFILE(");
            strSql.Append("BRANCH_ID,FILE_ID)");

            strSql.Append(" values (");
            strSql.Append("@BRANCH_ID,@FILE_ID)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.String, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "FILE_ID", DbType.String, model.FILE_ID);
            int rows = db.ExecuteNonQuery(dbCommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(int ID, int BRANCH_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_BRANCHFILE set ");
            strSql.Append("BRANCH_ID=BRANCH_ID+',@BRANCH_ID',");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.String, BRANCH_ID);
            int rows = db.ExecuteNonQuery(dbCommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID, string branchID, bool flag)
        {
            if (flag)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_PUB_BRANCHFILE set ");
                strSql.Append("BRANCH_ID=REPLACE(BRANCH_ID,',@BRANCH_ID','')");
                strSql.Append(" where ID=@ID");
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
                db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, branchID);
                db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
                int rows = db.ExecuteNonQuery(dbCommand);
                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("delete from T_PUB_BRANCHFILE ");
                strSql.Append(" where ID=@ID ");
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
                db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
                int rows = db.ExecuteNonQuery(dbCommand);
                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BRANCH_ID,FILE_ID ");
            strSql.Append(" FROM T_PUB_BRANCHFILE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.Progect.T_PUB_BRANCHFILE> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BRANCH_ID,FILE_ID ");
            strSql.Append(" FROM T_PUB_BRANCHFILE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.Progect.T_PUB_BRANCHFILE> list = new List<Model.Progect.T_PUB_BRANCHFILE>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.Progect.T_PUB_BRANCHFILE ReaderBind(IDataReader dataReader)
        {
            Model.Progect.T_PUB_BRANCHFILE model = new Model.Progect.T_PUB_BRANCHFILE();
            object ojb;
            ojb = dataReader["ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ID = (int)ojb;
            }
            model.BRANCH_ID = dataReader["BRANCH_ID"].ToString();
            model.FILE_ID = dataReader["FILE_ID"].ToString();
            return model;
        }

        #endregion  Method
    }
}
